package com.etboot.modules.base.serviceimpl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etboot.modules.base.dao.mapper.MessageSendMapper;
import com.etboot.modules.base.entity.MessageSend;
import com.etboot.modules.base.mpbase.mapper.BaseDao;
import com.etboot.modules.base.service.MessageSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 消息发送接口实现
 * @TODO 消息发送，代码被注释，暂时未实现
 * @author xboot
 */
@Slf4j
@Service
@Transactional
public class MessageSendServiceImpl extends ServiceImpl<MessageSendMapper, MessageSend> implements MessageSendService {
// public class MessageSendServiceImpl implements MessageSendService {
    @Autowired
    private MessageSendMapper messageSendMapper;

    // @Autowired
    // private SimpMessagingTemplate messagingTemplate;

    // @Override
    // public BaseDao<MessageSend> getDao() {
    //     return messageSendMapper;
    // }

    @Override
    public MessageSend send(MessageSend messageSend) {
        messageSendMapper.insert(messageSend);
        // messagingTemplate.convertAndSendToUser(messageSend.getUserId(), "/queue/subscribe", "您收到了新的消息");
        return messageSend;
    }

    @Override
    public void deleteByMessageId(String messageId) {
        messageSendMapper.delete(Wrappers.<MessageSend>lambdaQuery().eq(MessageSend::getMessageId,messageId));
    }

    @Override
    public Page<MessageSend> findByCondition(MessageSend messageSend, Page pageable) {
        QueryWrapper<MessageSend> qw = new QueryWrapper<>();

        if(StrUtil.isNotBlank(messageSend.getMessageId())){
            qw.eq("message_id", messageSend.getMessageId());
        }
        if(StrUtil.isNotBlank(messageSend.getUserId())){
            qw.eq("user_id", messageSend.getUserId());
        }
        if(messageSend.getStatus()!=null){
            qw.eq("status", messageSend.getStatus());
        }
        return messageSendMapper.selectPage(pageable,qw);
    }
}